package com.gwnet.rider.service.impl;

import com.gwnet.cabinet.domian.base.Result;
import com.gwnet.cabinet.domian.bo.AdminOpenBO;
import com.gwnet.cabinet.domian.bo.CabinetBoxOpenBO;
import com.gwnet.cabinet.enums.OpenTypeEnum;
import com.gwnet.cabinet.service.CabinetBoxService;
import com.gwnet.rider.domain.bo.CancelSetBoxBO;
import com.gwnet.rider.domain.bo.SetBoxBO;
import com.gwnet.rider.service.BoxService;
import com.gwnet.shop.domain.OrderDelivery;
import com.gwnet.shop.domain.OrderInfo;
import com.gwnet.shop.enums.DevStatusEnum;
import com.gwnet.shop.enums.OrderStatusEnum;
import com.gwnet.shop.service.IOrderDeliveryService;
import com.gwnet.shop.service.IOrderInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 外卖员操作外卖柜
 * @author: WangLiang
 * @date: 2024/8/5 8:44
 */
@Service
public class BoxServiceImpl implements BoxService {

    private final CabinetBoxService cabinetBoxService;
    private final IOrderDeliveryService deliveryService;
    private final IOrderInfoService orderInfoService;

    public BoxServiceImpl(CabinetBoxService cabinetBoxService, IOrderDeliveryService deliveryService, IOrderInfoService orderInfoService) {
        this.cabinetBoxService = cabinetBoxService;
        this.deliveryService = deliveryService;
        this.orderInfoService = orderInfoService;
    }


    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public Boolean setBox(SetBoxBO setBoxBO) {

        CabinetBoxOpenBO openBO = new CabinetBoxOpenBO();
        BeanUtils.copyProperties(setBoxBO,openBO);
        // TODO 这里先这么转一下，订单ID和配送ID理应都得是Long类型的
        Long orderId = Long.parseLong(setBoxBO.getOrderId());
        openBO.setOrderId(orderId);
        OrderInfo orderInfo = orderInfoService.getById(setBoxBO.getOrderId());

        // 修改配送表状态
        OrderDelivery delivery = deliveryService.getById(orderInfo.getDeliveryId());
        delivery.setDeliveryStatus(DevStatusEnum.WAIT_TAKE_DEV.code());
        deliveryService.updateById(delivery);

        // 修改主表状态
//        orderInfo.setOrderStatus(OrderStatusEnum.WAIT_TAKE_DEV.code());
//        orderInfoService.updateById(orderInfo);

        // 外卖员开柜放入外卖
        Boolean flag = cabinetBoxService.putToBox(openBO);

        return flag;
    }

    @Override
    public Boolean cancelSetBox(CancelSetBoxBO cancelSetBoxBO) {
        // 修改主表状态
        OrderInfo orderInfo = orderInfoService.getById(cancelSetBoxBO.getOrderId());
        // 状态回滚到上个状态 待送达
        orderInfo.setOrderStatus(OrderStatusEnum.WAIT_DELIVERY.code());
        orderInfoService.updateById(orderInfo);

        // 修改配送表状态
        OrderDelivery delivery = deliveryService.getById(orderInfo.getDeliveryId());
        // 状态回滚到上个状态 待送达
        delivery.setDeliveryStatus(DevStatusEnum.WAIT_DELIVERY.code());

        // 订单箱门编号，取件码，重置
        Boolean resetFlag = orderInfoService.cleanOrderBox(orderInfo.getOrderId());

        // 调用外卖柜服务，清柜并开门
        AdminOpenBO adminOpen = new AdminOpenBO();
        adminOpen.setDevSn(orderInfo.getDevSn());
        adminOpen.setBoxChs(String.valueOf(orderInfo.getBoxNo()));
        // 1：仅开箱，2：仅清箱，3：清箱并开箱，4：锁箱，5：解锁 9:打开箱门B面
        adminOpen.setOpenType(3);
        Boolean flag = cabinetBoxService.adminOpen(adminOpen);
        return resetFlag;
    }


}
